package ru.st.selenium.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import ru.st.selenium.util.MyUtil;

/**
 * Created with IntelliJ IDEA.
 * User: gor
 * Date: 20.10.13
 * Time: 19:19
 * To change this template use File | Settings | File Templates.
 */
public class LoginPage {


    public static void doLogin(WebDriver driver, String login, String password){

        if (MyUtil.isElementPresent(driver, By.xpath("//a[@href='http://localhost/php4dvd/?logout']"))){
            System.out.println("Already Logged In!");
            //  throw ?
            return;
        }

        driver.findElement(By.name("username")).clear();
        driver.findElement(By.name("username")).sendKeys(login);

        driver.findElement(By.name("password")).clear();
        driver.findElement(By.name("password")).sendKeys(password);

        driver.findElement(By.name("submit")).click();

        if (MyUtil.isElementPresent(driver, By.xpath("//a[@href='http://localhost/php4dvd/?logout']"))){
            System.out.println("OK, Logged In");
        }
        else {
            System.out.println("Failure!");
            throw new RuntimeException("Login Failure!");
        }
    }


}
